package com.pcbly.web.components.utils;

import java.util.LinkedHashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;

import com.pcbly.web.bean.OnlineUser;
import com.pcbly.web.bean.user.UserBean;

public class LightsoftUtils implements ApplicationContextAware {
	private static Log logger = LogFactory.getLog(LightsoftUtils.class);

	private static Map<String, OnlineUser> onlineUsers = new LinkedHashMap<String, OnlineUser>();
	private static ApplicationContext appContext;

	@Override
	public void setApplicationContext(ApplicationContext applicationcontext)
			throws BeansException {
		// TODO Auto-generated method stub
		if (logger.isDebugEnabled()) {
			logger.debug("init LightsoftUtils.....");
		}
		appContext = applicationcontext;
	}

	public static Object getBean(String s) {
		return appContext.getBean(s);
	}

	public static Map<String, OnlineUser> getOnlineUsers() {
		return onlineUsers;
	}

	public static void removeOnlineUser(String s) {
		onlineUsers.remove(s);
	}

	public static void addOnlineUser(String s, UserBean user) {
		OnlineUser onlineuser = new OnlineUser();
		onlineuser.setSessionId(s);
		onlineuser.setUsername(user.getUserName());
		onlineuser.setUserId(user.getId());
		onlineUsers.put(s, onlineuser);
	}

}
